package com.ldc.common.mvp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

@Deprecated

public abstract class BaseActivity<B extends ViewBinding, P extends BasePresenter> extends AppCompatActivity implements IBaseView {
    protected Activity activity;
    protected B mBinding;
    protected P mPresenter;
    protected final String TAG = getClass().getName();
    //ui消息处理器

    private final Handler.Callback callback = this::uiHandleMessage;
    private final Handler handler = new Handler(Looper.getMainLooper(), callback);

    public Handler getHandler() {
        return handler;
    }

    @Override

    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unBinding();
        }
        handler.removeCallbacksAndMessages(null);
    }


    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            activity = this;
            //强制竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            mBinding = initViewBinding();
            mPresenter = initPresenter();
            if (null != mPresenter) {
                mPresenter.onBinding(this);
                getLifecycle().addObserver(mPresenter);
            }
            handleIntent(getIntent());
            initView();
            initData();
        } catch (Exception e) {
            show_error(e);
        }
    }


    protected abstract B initViewBinding();

    protected abstract int ui();

    protected abstract P initPresenter();

    protected abstract void initView();

    protected abstract void initData();

    protected void handleIntent(Intent it) {
    }

    //处理消息
    protected boolean uiHandleMessage(Message msg) {
        return true;
    }

    protected void show_error(Exception e) {
        Log.d(TAG, String.format("show_error: %s", e.getMessage()));
    }


}
